/*
 * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
 */
package cn.player.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

import cn.player.view.ViewHolder;

public abstract class CommonAdapter<T> extends BaseAdapter {

    public Context mContext;
    public List<T> mData;
    public int layoutResource;

    public CommonAdapter(Context context, List<T> data) {
        super();
        this.mContext = context;
        this.mData = data;
    }

    /**
     * @param mContext       Context
     * @param mData          列表数据集合
     * @param layoutResource itemView资源
     */
    public CommonAdapter(Context mContext, List<T> mData, int layoutResource) {
        super();
        this.mContext = mContext;
        this.mData = mData;
        this.layoutResource = layoutResource;
    }

    public void setData(List<T> mData) {
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mData != null ? mData.get(position) : position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 设置itemView资源
     *
     * @param layoutResource
     */
    public void setLayoutResource(int layoutResource) {
        this.layoutResource = layoutResource;
    }

    /**
     * 更新数据
     *
     * @param viewHolder
     * @param bean
     */
    protected abstract void convertView(ViewHolder viewHolder, T bean);


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.getInstance(mContext, convertView, layoutResource, position);
        convertView(viewHolder, mData.get(position));
        return viewHolder.getConvertView();
    }

}
